/**
 * @(#) OverView.as
 *
 * @date		2009. 04. 20.
 * @version		1.0
 * @author		Jo, ChangHun
 * @description	연말정산 메인 view - Mediator
 * 				연말정산 - 탭3 인적기본공제 팝업창 Logic을 관리 
 */
package com.skt.common_hr.yearadjustment.view
{
	import com.skt.common_hr.common.PsHrAlert;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.yearadjustment.YearAdjustmentFacade;
	import com.skt.common_hr.yearadjustment.model.EduComboProxy;
	import com.skt.common_hr.yearadjustment.model.EducationalProxy;
	import com.skt.common_hr.yearadjustment.model.vo.EducationalVO;
	import com.skt.common_hr.yearadjustment.view.components.popEducational;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	

	public class popEducationalMediator extends CommonMediator
	{
		public static const NAME:String = "popEducationalMediator"; 
		public var v_year:String = "";
		[Bindable] public var v_enable:Boolean = true;
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function popEducationalMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
            view.callLater(initView);
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		public function initView():void
 		{
 			view.addEventListener(popEducational.CancelInput, onCancelInput);
 			view.addEventListener(popEducational.OkInput, onOkInput);
 			view._isEnable = v_enable;
 			selectComboData();
 		}
 		
 		private function selectComboData():void{
 			var proxy:EduComboProxy = facade.retrieveProxy(EduComboProxy.NAME) as EduComboProxy;
 			proxy.comboload();
 		}
 		
 		private function selectPopData():void{
 			var proxy:EducationalProxy = facade.retrieveProxy(EducationalProxy.NAME) as EducationalProxy;
 			proxy.load(Variables.yearPernr, v_year);
 		}
 		
 		private function filterEduCode(arr:ArrayCollection):ArrayCollection
 		{
 			var retArray:ArrayCollection = new ArrayCollection();
 			
 			for each(var vo:Object in arr) {
 				if(vo.CODE != "4") {
 					retArray.addItem(vo);
 				}
 			}	
 			return retArray;
 		}
 		
 		private function onOkInput(event:Event):void{
 			
 			var proxy:EducationalProxy = facade.retrieveProxy(EducationalProxy.NAME) as EducationalProxy;
 			
 			var detailList:Array = new Array();
	 		for each(var rec:EducationalVO in view.educationalData)
	 		{
	 			trace("YEASUB : " + rec.YEASUB);
	 			detailList.push(rec);
	 		}
 			
 			proxy.save(Variables.yearPernr, v_year, detailList);
 		}
 		
 		private function onCancelInput(event:Event):void
 		{
 			facade.removeMediator(popEducationalMediator.NAME);
 			PopUpManager.removePopUp(view);
 		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array
		{
			return [
					YearAdjustmentFacade.LOAD_EDUGUBUNCOMBO_COMPLETE,
					YearAdjustmentFacade.LOAD_EDUGUBUNCOMBO_FAILED,
					YearAdjustmentFacade.LOAD_EDU_LIST_COMPLETE,
					YearAdjustmentFacade.LOAD_EDU_LIST_FAILED,
					YearAdjustmentFacade.SAVE_EDU_LIST_COMPLETE,
					YearAdjustmentFacade.SAVE_EDU_LIST_FAILED
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case YearAdjustmentFacade.LOAD_EDUGUBUNCOMBO_COMPLETE:
					trace("YearAdjustmentFacade.LOAD_EDUGUBUNCOMBO_COMPLETE");
					view.comboGubun = filterEduCode((note.getBody() as EduComboProxy).comboGubun);
					selectPopData();
					break;
				case YearAdjustmentFacade.LOAD_EDUGUBUNCOMBO_FAILED:
					trace("YearAdjustmentFacade.LOAD_EDUGUBUNCOMBO_FAILED");
					break;	
				case YearAdjustmentFacade.LOAD_EDU_LIST_COMPLETE:
					trace("YearAdjustmentFacade.LOAD_EDU_LIST_COMPLETE");
					view.educationalData = (note.getBody() as EducationalProxy).eduData;
					break;
				case YearAdjustmentFacade.LOAD_EDU_LIST_FAILED:
					trace("YearAdjustmentFacade.LOAD_EDU_LIST_FAILED");
					break;	
				case YearAdjustmentFacade.SAVE_EDU_LIST_COMPLETE:
					trace("YearAdjustmentFacade.SAVE_EDU_LIST_COMPLETE");
					//regMediator();
					facade.sendNotification(YearAdjustmentFacade.RELOAD_BASIC_DATA);
					onCancelInput(null);
					break;
				case YearAdjustmentFacade.SAVE_EDU_LIST_FAILED:
					PsHrAlert.showMessage((note.getBody() as EducationalProxy).msgStr);
					trace("YearAdjustmentFacade.SAVE_EDU_LIST_FAILED");
					break;	
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
 		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():popEducational
        {
            return viewComponent as popEducational;
        }
	}
}